﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.AttributeFile;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;
using System.Collections.Generic;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.出库管理
{
    /// <summary>
    /// 出库
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class TShippingOrderDetailController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        private readonly ITInventoryRepository tInventory;
        private readonly ITShippingLockCellRepository tShippingLockCell;
        private readonly ITWarehouseRepository tWarehouse;
        private readonly IAreaRepository area;

        public ITShippingOrderDetailRepository _service { get; set; }

        public TShippingOrderDetailController(
            ITShippingOrderDetailRepository service,
            ITInventoryRepository tInventory,
            ITShippingLockCellRepository tShippingLockCell,
            ITWarehouseRepository tWarehouse,
            IAreaRepository area,
            ILogRepository logRepository
            )
        {
            _service = service;
            this.logRepository = logRepository;
            this.tInventory = tInventory;
            this.tShippingLockCell = tShippingLockCell;
            this.tWarehouse = tWarehouse;
            this.area = area;
        }

       
        /// <summary>
        /// 添加出库表 多个物料版本
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("AddTShippingOrderDetailPlus")]
        //[HttpIdempotent(WaitMillisecond = 10000, CacheMillisecond = 3000)]
        public IActionResult AddTShippingOrderDetailPlus(TShippingOrderDetailPlusDTO model)
        {
            var create = _service.AddTShippingOrderDetailPlus(model,out var msg);
            if (create)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "出库管理";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了出库单" + model.OrderSn;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            TLog loggErr = new TLog();
            loggErr.LogType = 1;
            loggErr.ActionType = 4;
            loggErr.ModuleName = "出库管理";
            loggErr.Description = msg;
            logRepository.AddTLod(loggErr);
            return Ok(BaseResult.Result(false, msg, "500"));
        }

        /// <summary>
        /// 添加出库表 多个物料版本
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("AddTShippingOrderDetailNew")]
        //[HttpIdempotent(WaitMillisecond = 10000, CacheMillisecond = 3000)]
        public IActionResult AddTShippingOrderDetailNew(TShippingOrderDetailPlusDTO model)
        {
            var create = _service.AddTShippingOrderDetailNew(model, out var msg);
            if (create)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "出库管理";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了出库单" + model.OrderSn;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            TLog loggErr = new TLog();
            loggErr.LogType = 1;
            loggErr.ActionType = 4;
            loggErr.ModuleName = "出库管理";
            loggErr.Description = msg;
            logRepository.AddTLod(loggErr);
            return Ok(BaseResult.Result(false, msg, "500"));
        }

        /// <summary>
        /// 显示出库表
        /// </summary>
        /// <param name="billType"></param>
        /// <param name="houseid"></param>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("GetAllTShippingOrderDetailPlus")]
        public IActionResult GetAllTShippingOrderDetailPlus(int? billType, int? houseid, string? name, int? statusNum, int pageIndex = 1, int pageSize = 8)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = _service.GetAllTShippingOrderDetailPlus(billType, houseid, name, pageIndex, pageSize, out totalCount, statusNum);
            if (list.Count >= 0)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 显示单个出库表
        /// </summary>
        /// <param name="billType"></param>
        /// <param name="houseid"></param>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("GetOneTShippingOrderDetailPlus")]
        public IActionResult GetOneTShippingOrderDetailPlus()
        {
            var resObje = _service.GetOneTShippingOrderDetailPlus(out bool res);
            if (res && resObje!=null)
            {
                return Ok(BaseResult.Result(resObje, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "200", 0));
        }

        /// <summary>
        /// 修改出库单状态为已回传
        /// </summary>
        /// <returns></returns>
        [HttpPut("UpdoutShippingStatus")]
        public IActionResult UpdoutShippingStatus(string orderSn)
        {
            var res = _service.UpdOutShippingStatus(orderSn,out var errorMsg);
            if(res)
                return Ok(BaseResult.Result(true, "修改成功", "200",0));
            return Ok(BaseResult.Result(false, "修改失败："+errorMsg, "500", 0));
        }
    }
}